<template>
	<view style="display: flex;margin:0 auto;flex-direction: column; width: 749rpx;">
		<!-- 固定图标的添加按键 -->
		<view @click="xianshi()" v-if="addBi"
			style="position:fixed;top: 500rpx;z-index: 100; right: 30rpx;border-radius: 50%;background-color:blue;display: flex;justify-content: center;align-items: center;width: 100rpx;height: 100rpx;">
			<image style="width: 80rpx;height: 80rpx;" src="../../static/10.png" mode=""></image>
		</view>

		<!-- 搜索框 -->
		<view
			style="display:flex;width: 700rpx ;background-color:white;align-items: center;justify-content: center;margin: 10rpx auto;border-radius:40rpx;">
			<u-search bgColor="white" margin="0 30rpx 0 0" :placeholder="shu2" actionText="搜索" v-model="soukey"
				@search="kechengfun" @custom="kechengfun"></u-search>
		</view>

		<!-- 横线隔开 -->
		<view style="display: flex;width: 90%;background-color: #DFDFDF;height: 3rpx;margin: 20rpx auto 10rpx auto;">
		</view>


		<!-- 最上面校区选择 -->
		<u-tabs style="height: 80rpx;" :current="current" :list="xiaoqu" scroll-y="true" lineWidth="50" lineColor="blue"
			:activeStyle="{ color: 'black',  transform: 'scale(1)'}"
			:inactiveStyle="{color: '#606266',transform: 'scale(0.9)'}"
			itemStyle="padding-left: 15px; padding-right: 15px; height: 34px;" @change="change1">
		</u-tabs>
		<!--课程 的展示 -->
		<view v-for="(item,index) in kechenglist" :key=index
			style="display: flex;flex-direction: row;padding: 0 20rpx; align-items: center;justify-content: space-around;">
			<view
				style="display: flex;border-radius: 20rpx; width:90%;background-color: white;box-shadow: 1px 1px 5px #c2c2c2;margin: 20rpx 0 0 20rpx;padding:15rpx;justify-content: space-between;align-items: center;">
				<view @click="xiugaikecheng_show(item)"
					style="display: flex;flex-direction: column;margin-left: 30rpx;">
					<view>
						{{item.name}}
					</view>
					<view style="color: #c2c2c2;font-size: 28rpx;">
						{{item.laoshi}}--{{item.xiaoqu}}
					</view>
					<view v-if="meiyi_userinfo.permission.indexOf('get_keshifei') != -1" style="color: #c2c2c2;font-size: 28rpx;">
						课时费:{{item.keshifeitolaoshi}}，分类:{{item.fenleiname}}
					</view>
					<view v-if="!item.fenleiname" style="color: red;font-size: 28rpx;">
						无分类
					</view>
					<view v-if="!item.keshifeitolaoshi" style="color: red;font-size: 28rpx;">
						无课时费
					</view>
					<!-- <view  style="color: #c2c2c2;font-size: 28rpx;">
						教务老师：{{item.jiaowulaoshi}}
					</view> -->
				</view>
				<view style="display: flex;color: #c2c2c2;font-size: 28rpx;margin-right: 30rpx;"
					@click="xueshengs(item)">
					学员情况>>
				</view>
			</view>
		</view>
		<!-- 下面是添加新课程出现的弹窗 -->
		<model v-if='show5' @colseModel='show5 = false'>
			<view
				style="display: flex;background-color:blue;color: white;border-radius: 20rpx;padding: 15rpx ; margin: 25rpx auto;justify-content: center;">
				给【{{shu3}}】添加新课程！
			</view>
			<view style="display: flex;margin-bottom: 30rpx;">示例：拉丁张三(周日8:00-10:00)</view>
			<uni-easyinput type="text" v-model="shu8" placeholder="按上面示例输入课程名称" />
			<view style="display: flex;margin-bottom: 30rpx;">选择任课老师名字</view>
			<picker @change="bindPickerChange" :range="shu7">
				<uni-easyinput type="text" disabled v-model="renkelaoshi" suffixIcon="bottom" />
			</picker>
			<view v-if="meiyi_userinfo.permission.indexOf('update_keshifei') != -1" style="display: flex;margin-bottom: 30rpx;">老师课时费</view>
			<uni-easyinput v-if="meiyi_userinfo.permission.indexOf('update_keshifei') != -1" type="number" v-model="kecheng_4" />
			<view style="display: flex;margin-bottom: 30rpx;">课程所属分类</view>
			<picker @change="xuanze_kechengfenlei_fun" :range="kecheng_fenlei_namelist">
				<view  style="display: flex;border: #c2c2c2 3rpx solid;padding: 5rpx 8rpx;width:650rpx;height: 40rpx;">
					{{kechengfenlei_name}}
				</view>
			</picker>
			<view style="display: flex;width: 60%;justify-content: space-around;margin: 30rpx auto;color: white;">
				<view style="display: flex;background-color: blue;border-radius: 15rpx;padding: 20rpx 60rpx;"
					@click="show5 = false">取消</view>
				<view style="display: flex;background-color: blue;border-radius: 15rpx;padding: 20rpx 60rpx;"
					@click="addkecheng()">确定</view>
			</view>
		</model>

		<!-- 下面是修改课程出现的弹窗 -->
		<model v-if='show6' @colseModel='show6 = false'>
			<view
				style="display: flex;background-color:blue;color: white;border-radius: 20rpx;padding: 15rpx ; margin: 25rpx auto;justify-content: center;">
				给【{{shu3}}】修改课程信息！
			</view>
			<view style="display: flex;margin-bottom: 30rpx;">输入新课程名字</view>
			<uni-easyinput type="text" v-model="xinkecheng" />
			<view style="display: flex;margin-bottom: 30rpx;">选择任课老师名字</view>
			<picker @change="bindPickerChange" :range="shu7">
				<uni-easyinput type="text" disabled v-model="laoshiname" suffixIcon="bottom" />
			</picker>
			<view style="display: flex;margin-bottom: 30rpx;">任课老师电话</view>
			<uni-easyinput type="text" disabled v-model="laoshidianhua" />
			<view v-if="meiyi_userinfo.permission.indexOf('update_keshifei') != -1" style="display: flex;margin-bottom: 30rpx;">老师课时费</view>
			<uni-easyinput v-if="meiyi_userinfo.permission.indexOf('update_keshifei') != -1" type="number" v-model="kecheng_4" />
			<view style="display: flex;margin-bottom: 30rpx;">课程所属分类</view>
			<picker @change="xuanze_kechengfenlei_fun" :range="kecheng_fenlei_namelist">
				<view  style="display: flex;border: #c2c2c2 3rpx solid;padding: 5rpx 8rpx;width:650rpx;height: 40rpx;">
					{{kechengfenlei_name}}
				</view>
			</picker>
			<view style="display: flex;width: 60%;justify-content: space-around;margin: 30rpx auto;color: white;">
				<view style="display: flex;background-color: blue;border-radius: 15rpx;padding: 20rpx 60rpx;"
					@click="show6 = false,addBi=1">取消</view>
				<view style="display: flex;background-color: blue;border-radius: 15rpx;padding: 20rpx 60rpx;"
					@click="xiugaikecheng()">确定</view>
			</view>
			<view v-if="meiyi_userinfo.permission.indexOf('del_keshifei') != -1" @click="shanchukecheng(kecheng)"
				style="display: flex; margin-top: 40rpx;justify-content: center;">
				删除本课程！
			</view>
		</model>
	</view>
</template>

<script>
	import getDateTime from '@/common/getdateTime.js';
	import model from '../../components/model.vue'
	export default {
		components: {
			model
		},
		data() {
			return {
				details:'',
				kecheng_fenlei_namelist:[],
				kechengfenlei_name:'',
				laoshiname:'',
				laoshidianhua:'',
				addBi: 1,
				classroom: [],
				soukey: '', //搜索框 关键词
				kechenglist: {},
				jiaoshilist: [],
				xiaoqu: [], //去重后的校区
				xiaoqu1: [], //没有去重后的校区
				jiaowulaoshi: [],
				meiyi_userinfo: {
					Permissions:[]
				}, //判断登陆人信息
				imageStyles: {
					width: 100,
					height: 100,
					border: {
						color: "blue",
						width: 2,
						style: 'dashed',
						radius: '12px'
					}
				},
				current: 0, //UTAB的标签数字
				shu: '',
				shu2: '请输入课程名称进行搜索', //搜索框中显示的内容
				shu3: '', //添加教室和课程时，承载校区名称
				shu4: '', //校区的管理老师,添加老师电话承载
				shu5: '', //老师的电话
				shu7: [], //老师信息数组 名字+电话
				shu7name: [], //老师信息数组 名字
				shu7tel: [], //老师信息数组 电话
				shu8: '', //添加校区和教室和课程时临时传值
				shu15: '', //任课老师的ID
				shu15id: [],
				shu16: '', //添加教室时教务老师的ID
				shu16id: [],
				biaoqian1: true,
				biaoqian2: false,
				biaoqian3: false,
				renkelaoshi: '', //添加课程时的老师
				show: false, //输入校区名字
				show1: false, //输入新校区名字
				show3: false, //输入新教室名字，新添加时用
				show4: false, //输入新教室名字，更改时用
				show5: false, //输入新课程名字，新添加时用
				show6: false, //输入新课程字名，更改时用
				show7: false, //输入新老师名字，新添加时用
				show8: false, //输入新老师字名，更改时用
				tou10: true,
				tou20: false,
				tou30: false,
			}
		},
		async onLoad() {
			this.meiyi_userinfo = await uniCloud.getCurrentUserInfo()
			let meiyi_name_tel=await uni.getStorageSync('meiyi_name_tel')
			this.meiyi_name_tel=meiyi_name_tel
			this.jingshouren=meiyi_name_tel.name+meiyi_name_tel.mobile
			console.log('88',this.jingshouren);
			this.laoshi() //获取老师的数据
			this.jiaowulaoshifun() //获取教务老师的数据
			this.xiaoqufun()
			// this.get_kechengfenlei_list()
		},

		methods: {
			// 选择课程分类
			xuanze_kechengfenlei_fun: function(e) {
				this.kechengfenlei_name=this.kecheng_fenlei_namelist[e.detail.value]
				this.kecheng_fenlei=this.kechengfenlei_list[e.detail.value]
			},
			// 获得课程分类
			get_kechengfenlei_list(){
				uniCloud.callFunction({
					name:"funshare",
					data:{
						leixing:42,
					}
				}).then(res=>{
					this.kecheng_fenlei_namelist=[]
					for(var i=0;i<res.result.data.length;i++){
						this.kecheng_fenlei_namelist.push(res.result.data[i].name)
					}
					this.kechengfenlei_list=res.result.data
					for(var i=0;i<this.kechenglist1.length;i++){
						for(var ii=0;ii<this.kechengfenlei_list.length;ii++){
							if(this.kechenglist1[i].fenleiid=='未分类'){
								this.kechenglist1[i].fenleiname="未分类"
								break
							}
							if(this.kechenglist1[i].fenleiid==this.kechengfenlei_list[ii]._id){
								this.kechenglist1[i].fenleiname=this.kechengfenlei_list[ii].name
								break
							}
						}
					}
					this.kechenglist=this.kechenglist1
				})
			},
			// 获取我管理校区的课程
			kechengfun() {
				uni.showLoading({
					title: '课程信息获取…',
					mask: true
				});
				uniCloud.callFunction({
					name: "funshare",
					data: {
						leixing: 27, //获取我管理校区的课程
						xiaoqu: this.shu1,
						key: this.soukey

					}
				}).then(res => {
					this.kechenglist1 = res.result.data
					this.get_kechengfenlei_list()
					uni.hideLoading()
				})
			},
			// 获取我管理的校区
			async xiaoqufun() {
				const res = await uni.getStorageSync('aoyashengxiaoqu')
				this.xiaoqu1 = res.result.data
				this.xiaoqu = []
				let arr = []
				for (var i = 0; i < res.result.data.length; i++) {
					var cou = 0
					for (var j = i + 1; j < res.result.data.length; j++) {
						if (res.result.data[i].name == res.result.data[j].name) {
							cou = 9
							break
						}
					}
					if (cou == 0) {
						this.xiaoqu.push(res.result.data[i])
						arr.push(res.result.data[i].name)
					}
				}
				this.wodexiaoqu = arr
				if (this.xiaoqu.length > 1) {
					this.xiaoqu.unshift({
						name: '全部'
					})
				}else{
					this.shu3=this.xiaoqu[0].name
				}
				this.shu1 = this.wodexiaoqu
				uni.hideLoading()
				this.kechengfun()
			},
			//进入课程学生信息
			async xueshengs(e) {
				await uni.setStorageSync('aoyashengkecheng1', e)
				uni.navigateTo({
					url: '/pages/kechenghexiao/kechenghexiao'
				});
			},
			//调取某个校区的教室
			jiaoshilistfun() {
				uni.showLoading({
					title: '教室信息获取…',
					mask: true
				});
				uniCloud.callFunction({
					name: "funshare",
					data: {
						leixing: 23, //类型为7时，某个校区的教室
						weizhi: this.shu1,
						key: this.soukey
					}
				}).then(res => {
					this.jiaoshilist = res.result.data
					uni.hideLoading()
				})
			},
			// 教务老师的数据库调取
			jiaowulaoshifun() {
				uni.showLoading({
					title: '老师信息获取…',
					mask: true
				});
				uniCloud.callFunction({
					name: "funshare",
					data: {
						leixing: 10, //类型为7时，某个校区的教室
						role: 'renkelaoshi'
					}
				}).then(res => {
					res.result.data.map(item => {
						this.jiaowulaoshi.push(item.name + item.mobile)
						this.shu16id.push(item._id)
					})
					uni.hideLoading()
				})
			},


			// 图片上传成功
			fanhuiurl(e) {
				this.zhaopian = e.tempFiles[0].fileID
			},
			
			// 选择任课老师
			bindPickerChange: function(e) {
				if (this.show5) {
					this.renkelaoshi = this.shu7[e.detail.value]
					this.renkelaoshi_1 = this.shu7name[e.detail.value]
					this.renkelaoshi_2 = this.shu7tel[e.detail.value]
					this.shu15 = this.shu15id[e.detail.value]
				} else if (this.show6) {
					this.laoshiname = this.shu7name[e.detail.value]
					this.laoshidianhua = this.shu7tel[e.detail.value]
					this.shu15 = this.shu15id[e.detail.value]
				}
			},
			// 进入排课的界面
			async paike(e) {
				await uni.setStorageSync('aoyashengjiaoshi', e)
				uni.navigateTo({
					url: "/pages/jiaoshi4a/jiaoshi4a"
				})
			},
			
		
			// 修改课程，一、kecheng更新，二、xiugaishuju添加一条，三、baokelist中kecheng改为新的
			xiugaikecheng() {
				this.details=''
				uni.showLoading({
					title: '课程修改…',
					mask: true
				});
				let that = this
				if (this.kecheng.name == this.xinkecheng && this.kecheng.laoshi == this.laoshiname && this.kecheng
					.laoshitel == this.laoshidianhua && this.kecheng.keshifeitolaoshi == this.kecheng_4 && this.kecheng
					.jiaowulaoshi == this.kecheng_5 &&this.kecheng.fenleiid==this.kecheng_fenlei._id ) {
					this.show6 = false
					this.addBi = 1
					uni.hideLoading()
					return uni.showToast({
						title: '未修改数据！',
						icon: 'none',
						duration: 1000
					})
				}
				if (!this.xinkecheng) {
					uni.showToast({
						title: '请输入课程名称！',
						icon: 'none',
						duration: 1000
					})
					uni.hideLoading()
					return
				}
				if (!this.kechengfenlei_name) {
					uni.showToast({
						title: '请选择课程分类！',
						icon: 'none',
						duration: 1000
					})
					uni.hideLoading()
					return
				}
				if(this.kecheng.name!==this.xinkecheng){
					this.details=this.details+'课程名称：【' + this.kecheng.name + '】→【' + this.xinkecheng + '】'
				}
				if(this.kecheng.laoshi!== this.laoshiname){
					this.details=this.details+'任课老师：【' + this.kecheng.laoshi + this.kecheng.laoshitel + '】→【' + this.laoshiname + this.laoshidianhua +'】'
				}
				if( this.kecheng.keshifeitolaoshi!==this.kecheng_4 ){
					this.details=this.details+'课时费：【' + this.kecheng.keshifeitolaoshi + '】→【' + this.kecheng_4 + '】'
				}
				if(this.kecheng.jiaowulaoshi!==this.kecheng_5){
					this.details=this.details+'教务老师：【' + this.kecheng.jiaowulaoshi + '】→【' + this.kecheng_5 + '】'
				}
				if(this.kecheng.fenleiname!==this.kechengfenlei_name){
					this.details=this.details+'分类：【'+this.kecheng.fenleiname+'】→【'+this.kechengfenlei_name+'】'
				}
				uniCloud.callFunction({
					name: "funshare",
					data: {
						leixing: 24, //类型为7时，某个校区的教室
						id: this.shu,
						name: this.xinkecheng,
						xiaoqu: this.kecheng.xiaoqu,
						yuankecheng: this.kecheng.name,
						detail:this.details,
						riqi: getDateTime.dateTimeStr('y-m-d h:i:s'),
						jingshouren: this.jingshouren,
						fenleiid:this.kecheng_fenlei._id,
						laoshi: this.laoshiname,
						laoshitel: this.laoshidianhua,
						keshifeitolaoshi: parseFloat(this.kecheng_4),
						jiaowulaoshi: this.kecheng_5,
						laoshiid: this.shu15
					}
				}).then(res => {
					uni.hideLoading()
					this.kechengfun()
					this.show6 = false
					this.addBi = 1
				})
			},
			// 修改课程时弹窗
			xiugaikecheng_show(e) {
				this.kecheng = e
				this.xinkecheng = e.name
				this.laoshiname = e.laoshi
				this.laoshidianhua = e.laoshitel
				this.kecheng_4 = e.keshifeitolaoshi
				this.kecheng_5 = e.jiaowulaoshi
				this.shu = e._id
				this.shu3 = e.xiaoqu
				this.show6 = true
				this.addBi = 0
				this.kechengfenlei_name=e.fenleiname
				this.kecheng_fenlei={_id:e.fenleiid}
				this.shu15 = e.laoshiid
			},
			// 删除课程，一、kecheng中删除一条，二、xiugaishuju中添加一条
			shanchukecheng(item) {
				uni.showModal({
					title: '重要提示，删除课程：' + item.name,
					content: '再次确认，一定要删除吗？再次确认，一定要删除吗？再次确认，一定要删除吗？再次确认，一定要删除吗？再次确认，一定要删除吗？再次确认，一定要删除吗？',
					showCancel: true,
					cancelText: '取消',
					confirmText: '确定',
					success: res => {
						if (res.confirm) {
							uni.showLoading({
								title: '删除中…',
								mask: true
							});
							uniCloud.callFunction({
								name: "delpic",
								data: {
									leixing: 8, //类型为8时，删除指定课程
									id: item._id,
									detail: '删除课程，校区：【' + item.xiaoqu + '】，课程：【' + item.name +
										'】，任课老师：【' + item.laoshi + item.laoshitel + '】',
									riqi: getDateTime.dateTimeStr('y-m-d h:i:s'),
									jingshouren: this.jingshouren,
									xiaoqu: item.xiaoqu
								}
							}).then(res => {
								this.show6 = false
								this.addBi = 1
								this.kechengfun()
								uni.showToast({
									title: '删除成功！',
									icon: 'none',
									duration: 1000
								})
								uni.hideLoading()
							})
						}
					},
					fail: () => {},
					complete: () => {}
				});
			},
			
			// 添加新课程
			addkecheng() {
				uni.showLoading({
					title: '课程添加…',
					mask: true
				});
				if (!this.shu8) {
					uni.showToast({
						title: '请输入课程名称！',
						icon: 'none',
						duration: 1000
					})
					uni.hideLoading()
					return
				}
				if (!this.renkelaoshi) {
					uni.showToast({
						title: '请选择任课老师！',
						icon: 'none',
						duration: 1000
					})
					uni.hideLoading()
					return
				}
				if (!this.kechengfenlei_name) {
					uni.showToast({
						title: '请选择课程分类！',
						icon: 'none',
						duration: 1000
					})
					uni.hideLoading()
					return
				}
				if (!this.shu3) {
					uni.showToast({
						title: '无可管理校区，联系管理员！',
						icon: 'none',
						duration: 1000
					})
					uni.hideLoading()
					return
				}
				uniCloud.callFunction({
					name: "weifun",
					data: {
						leixing: 0, //添加新课程
						name: this.shu8,
						laoshi: this.renkelaoshi_1,
						xiaoqu: this.shu3,
						laoshitel: this.renkelaoshi_2,
						laoshiid: this.shu15,
						fenleiid:this.kecheng_fenlei._id,
						keshifeitolaoshi:this.kecheng_4?this.kecheng_4:0,
						// 课时费在云函数已自动输入0
						detail: '添加新课程，课程名称：【' + this.shu8 + '】，任课老师：【' + this.renkelaoshi + '】',
						riqi: getDateTime.dateTimeStr('y-m-d h:i:s'),
						jingshouren: this.jingshouren
					}
				}).then(res => {
					this.show5 = false
					uni.showToast({
						title: '添加成功！',
						icon: 'none',
						duration: 1000
					})
					uni.hideLoading()
					this.kechengfun()
				})
			},
			
			
			//课程管理中进行切换
			change1(e) {
				this.shu1 = []
				if (e.name == '全部') {
					this.shu1 = this.wodexiaoqu
				} else {
					this.shu1.push(e.name)
				}
				this.shu3 = e.name
				this.kechengfun()
			},
			// 任课老师的数据库调取
			laoshi() {
				uni.showLoading({
					title: '数据获取中……',
					mask: true
				});
				uniCloud.callFunction({
					name: "funshare",
					data: {
						leixing: 10, //获取任课老师列表
						role: 'renkelaoshi'
					}
				}).then(res => {
					this.shu7 = []
					this.shu7name = []
					this.shu7tel = []
					this.shu15id = []
					res.result.data.map(item => {
						this.shu7.push(item.name + item.mobile)
						this.shu7name.push(item.name)
						this.shu7tel.push(item.mobile)
						this.shu15id.push(item._id)
					})
					uni.hideLoading()
				})
			},
			
			// 下面是填加课程时显示的弹窗
			xianshi() {
				if (this.meiyi_userinfo.permission.indexOf('add_kecheng') == -1) {
					uni.showToast({
						title: '无添加课程权限，请联系管理员！',
						icon: 'none',
						duration: 2000
					})
					return
				}
				if (this.shu3 == '全部' || this.shu3 == '') {
					uni.showToast({
						title: '请选择校区后再次尝试！',
						icon: 'none',
						duration: 1000
					})
					return
				}
				//添加课程时出现弹窗
				if (this.xiaoqu.length > 0) {
					this.show5 = true
					this.shu8=''
					this.renkelaoshi=''
					this.kecheng_4=''
					this.kechengfenlei_name=''
				} else {
					uni.showToast({
						title: '没有管理的校区，请联系管理员添加校区！',
						icon: 'none',
						duration: 1000
					})
					return
				}
			},
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #f8f8f8;

		.xuanzhong {
			display: flex;
			width: 150rpx;
			padding: 9rpx 25rpx;
			justify-content: center;
			align-items: center;
			background-color: blue;
			border-radius: 15rpx;
			color: white;
		}

		.weixuanzhong {
			display: flex;
			width: 150rpx;
			padding: 9rpx 25rpx;
			justify-content: center;
			align-items: center;
			border-color: blue;
			border-width: 4rpx;
			border-style: solid;
			border-radius: 15rpx;
			color: blue;
		}
	}
</style>